package com.controller.hotel;

import com.biz.HotelAdminBiz;
import com.biz.ManagerInfoBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.HotelAdmin;
import com.entity.model.ManagerInfo;
import com.util.SystemConstant;
import com.util.VerifyCodeUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017-09-15.
 */
@Controller
public class HotelLoginController {

    @Autowired
    private HotelAdminBiz hotelAdminBiz;

    //登录页面
    @RequestMapping(value = "/hotel/loginIndex")
    public String toLogin() throws Exception {
        return "/hotel/login";
    }

    //登录验证
    @RequestMapping(value = "/hotel/loginCheck", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Result loginCheck(HttpSession session, @RequestParam String account, @RequestParam String password, @RequestParam String verifyCode) throws Exception {
        if (StringUtils.isBlank(account)) {
            return ResultGenerator.genFailResult("账号不能为空");
        }
        if (StringUtils.isBlank(password)) {
            return ResultGenerator.genFailResult("密码不能为空");
        }

        String code = (String) session.getAttribute(SystemConstant.SESSION_SECURITY_CODE);
        if (!verifyCode.equals(code)) {
            return ResultGenerator.genFailResult("验证码错误");
        }
        Condition cond = new Condition(ManagerInfo.class);
        password = DigestUtils.md5Hex(account + password);
        cond.createCriteria().andEqualTo("account", account).andEqualTo("password", password);
        List<HotelAdmin> list = hotelAdminBiz.findByCondition(cond);
        session.removeAttribute(SystemConstant.SESSION_SECURITY_CODE);
        if (list.size() == 0) {
            return ResultGenerator.genFailResult(-99, "账号或密码错误");
        }
        session.setAttribute(SystemConstant.SESSION_HOTEL_ADMIN, list.get(0));
        session.setMaxInactiveInterval(3600 * 24 * 30);
        HotelAdmin upHotelAdmin = new HotelAdmin();
        upHotelAdmin.setLoginTime(new Date());
        upHotelAdmin.setId(list.get(0).getId());
        hotelAdminBiz.update(upHotelAdmin);
        return ResultGenerator.genSuccessResult("/hotel/security/index");
    }

    //退出登录
    @RequestMapping("/hotel/security/loginOut")
    @ResponseBody
    public Result logout(HttpSession session) {
        session.invalidate();
        return ResultGenerator.genSuccessResult("/hotel/loginIndex");
    }

    @RequestMapping(value = "/hotel/captcha")
    public void captcha(HttpServletResponse response, HttpServletRequest request) {
        //设置页面不缓存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //为了手机客户端方便使用数字验证码
        String verifyCode = VerifyCodeUtil.generateTextCode(VerifyCodeUtil.TYPE_NUM_ONLY, 4, null);
        HttpSession session = request.getSession();
        session.setAttribute(SystemConstant.SESSION_SECURITY_CODE, verifyCode);
        try {
            //设置输出的内容的类型为JPEG图像
            response.setContentType("image/jpeg");
            BufferedImage bufferedImage = VerifyCodeUtil.generateImageCode(verifyCode, 90, 30, 3, true, Color.WHITE, Color.BLACK, null, null);
            //写给浏览器
            ImageIO.write(bufferedImage, "JPEG", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
